import { _polar, _mid, _dis, _vec, _move, _sin, _cos, _random } from "../../math/index.js"
import { _polygon, _square } from "../polygon.js"
import { _circle, _g } from "../svg.js"
import { polarPoints } from "../../math/points/polarPoints.js"
import { _mirror } from "../../math/mirror.js"
import { _colors } from "../../color/index.js"


const squarePoints = (options) => {
    let { o = [400, 300], r = 100, a = 0, vertex, fractal } = options
    let p = _polar(o, r, a)

    // 线段
    let points = [o, p]

    // 中点
    let mp = _mid(o, p)

    // 垂线
    let vPoints = points.map((t, i) => _polar(mp, r / 2, a + 90 + 180 * i))
    // 4个点
    points.push(..._move([p, o], vPoints[0], vPoints[1]))
    return points
}
// 菱形树叶
export const _leaf = (options, props) => {
    let { o = [400, 300], r = 100, a = 0, vertex, fractal, direct = 1 } = options
    let points = squarePoints(options)
    let children = []

    let square = _square({
        points
    }, {
        fill: 'none',
        stroke: 'green',
        ...props
    })

    children.push(square)


    // 顶点
    if (vertex) {
        let cirlces = points.map(t => {
            return _circle({
                o: t,
                r: 3
            }, {
                fill: 'blue'
            })
        })

        children.push(...cirlces)
    }

    // 分形迭代
    if (fractal) {

        if (fractal.fractalLevel < 0) return
        let angle = _random(30, 45) //45

        let leaf1 = _leaf({
            o: direct ? points[3] : points[2],
            r: r / 2,
            a: a - angle,
            fractal: {
                fractalLevel: fractal.fractalLevel - 1
            },
            direct: 1
        })

        let leaf2 = _leaf({
            o: direct ? points[2] : points[1],
            r: r / 2,
            a: a + angle,
            fractal: {
                fractalLevel: fractal.fractalLevel - 1
            },
            direct: 0
        })
        children.push(leaf1, leaf2)

    }
    return _g(children)
}


export const _leaves = (options, props) => {
    let { o = [400, 300], r = 100, a = 0, n = 6, assemble = true } = options
    let ps = polarPoints({
        o,
        r,
        n,
        a
    })

    return ps.map((t, i) => {
        return _leaf({
            ...options,
            o: assemble ? o : t,
            r,
            a: a + i * 360 / n,

        })
    })

}

const trianglePoints = (options) => {
    let { o = [400, 300], r = 100, a = 0 } = options
    let p = _polar(o, r, a)

    // 线段
    let points = [o, p]

    // 中点
    let mp = _mid(o, p)

    // 垂线
    let vPoints = points.map((t, i) => _polar(mp, r / 2, a + 90 + 180 * i))
    // 3个点
    points.push(..._move([p], vPoints[0], vPoints[1]))
    return points
}
// 三角叶
export const _triangleLeaf = (options, props) => {
    let { o = [400, 300], n = 3, r = 100, a = 0, vertex, fractal, direct = 1, assemble, color = "colorCircle" } = options
    let children = []
    let colors = _colors(color, n)
    const fn = ({ o, r, a, stroke }) => {
        let points = trianglePoints({
            o, r, a
        })
        let pg = _polygon({
            points
        }, {
            fill: 'none',
            stroke,
            // stroke: colors[] //'green',
            ...props
        })
        children.push(pg)
        // 顶点
        if (vertex) {
            let cirlces = points.map(t => {
                return _circle({
                    o: t,
                    r: 3
                }, {
                    fill: 'blue'
                })
            })

            children.push(...cirlces)
        }
        // return pg
    }

    let ps = polarPoints({
        o,
        r,
        n,
        a
    })
    ps.forEach((t, i) => fn({
        ...options,
        o: assemble ? o : t,
        r,
        a: a + i * 360 / n,
        stroke: colors[i]
    }))



    return _g(children)

}